// app.js
App({
  globalData: {
    userInfo: null,
    token: null,
    theme: wx.getSystemInfoSync()?.theme || 'light',
    systemInfo: wx.getSystemInfoSync()
  },

  onLaunch() {
    // 初始化云开发
    if (wx.cloud) {
      wx.cloud.init({
        env: 'cloud1-6gz10xdd7d0548eb',
        traceUser: true
      })
    } else {
      console.error('请使用 2.2.3 或以上的基础库以使用云能力')
    }

    // 检查登录状态
    const token = wx.getStorageSync('token');
    if (token) {
      this.globalData.token = token;
      this.getUserInfo();
    }

    // 监听系统主题变化
    wx.onThemeChange((result) => {
      this.globalData.theme = result.theme;
    });
  },

  getUserInfo() {
    wx.request({
      url: 'https://api.example.com/user/info',
      method: 'GET',
      header: {
        'Authorization': `Bearer ${this.globalData.token}`
      },
      success: (res) => {
        if (res.statusCode === 200) {
          this.globalData.userInfo = res.data;
          wx.setStorageSync('userInfo', res.data);
        }
      }
    });
  }
})
